package com.demo.zhou.sniper.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.demo.zhou.sniper.R;
import com.demo.zhou.sniper.application.MyApplication;
import com.demo.zhou.sniper.gen.DaoSession;
import com.demo.zhou.sniper.gen.QunbeanDao;
import com.demo.zhou.sniper.gen.ZubeanDao;
import com.demo.zhou.sniper.module.Qunbean;
import com.demo.zhou.sniper.module.Zubean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by zhou on 2017/6/18.
 */

public class NewqunActivity extends BaseActivity implements View.OnClickListener {
    private EditText newqun_et_name, newqun_et_xian;
    private Spinner newqun_sp_fangan;
    private Button newqun_id_hold, newqun_bt_cancel, newqun_bt_add;
    private static String qunName;
    private RadioButton newqun_rb_mosy, newqun_rb_moss;
    private TextView newqun_tv_id, newqun_tv_zunmb;
    private int zunmb = 1;
    DaoSession daoSession;
    ZubeanDao zubeanDao;
    QunbeanDao qunbeanDao;
    final List<String> fangAnList = new ArrayList<>();
    private String fangAnHao = "101";
    Qunbean qunbean;
    static Long qunIdToZu = 0l;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("新建群");
        setRightBtnVisible(false);
        into();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_newqun;
    }

    public void into() {
        newqun_et_name = (EditText) findViewById(R.id.newqun_et_name);
        newqun_et_xian = (EditText) findViewById(R.id.newqun_et_xian);
        newqun_sp_fangan = (Spinner) findViewById(R.id.newqun_sp_fangan);
        newqun_rb_mosy = (RadioButton) findViewById(R.id.newqun_rb_mosy);
        newqun_rb_moss = (RadioButton) findViewById(R.id.newqun_rb_moss);
        newqun_id_hold = (Button) findViewById(R.id.newqun_id_hold);
        newqun_tv_id = (TextView) findViewById(R.id.newqun_tv_id);
        newqun_tv_zunmb = (TextView) findViewById(R.id.newqun_tv_zunmb);
        newqun_id_hold.setOnClickListener(this);
        newqun_bt_cancel = (Button) findViewById(R.id.newqun_bt_cancel);
        newqun_bt_cancel.setOnClickListener(this);
        newqun_bt_add = (Button) findViewById(R.id.newqun_bt_add);
        newqun_bt_add.setOnClickListener(this);

        fangAnList.add("101");
        fangAnList.add("102");
        fangAnList.add("103");
        fangAnList.add("104");
        fangAnList.add("105");
        fangAnList.add("106");
        fangAnList.add("107");
        fangAnList.add("108");
        fangAnList.add("109");
        fangAnList.add("110");

        fangAnList.add("201");
        fangAnList.add("202");
        fangAnList.add("203");
        fangAnList.add("204");
        fangAnList.add("205");
        fangAnList.add("206");
        fangAnList.add("207");
        fangAnList.add("208");
        fangAnList.add("209");
        fangAnList.add("210");

        fangAnList.add("301");
        fangAnList.add("302");
        fangAnList.add("303");
        fangAnList.add("304");
        fangAnList.add("305");
        fangAnList.add("306");
        fangAnList.add("307");
        fangAnList.add("308");
        fangAnList.add("309");
        fangAnList.add("310");

        fangAnList.add("401");
        fangAnList.add("402");
        fangAnList.add("403");
        fangAnList.add("404");
        fangAnList.add("405");
        fangAnList.add("406");
        fangAnList.add("407");
        fangAnList.add("408");
        fangAnList.add("409");
        fangAnList.add("410");


        initClick();
        daoSession = ((MyApplication) getApplication()).getDaoSession();
        zubeanDao = daoSession.getZubeanDao();
        qunbeanDao = daoSession.getQunbeanDao();
        newqun_tv_zunmb.setText("当前序列号：" + zunmb++);
    }

    public void initClick() {
        newqun_sp_fangan.setAdapter(setAdapter(fangAnList));
        newqun_sp_fangan.setSelection(0, true);
        newqun_sp_fangan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                fangAnHao = fangAnList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.newqun_id_hold:
                saveQunId();
                break;
            case R.id.newqun_bt_cancel:
                finish();
                break;
            case R.id.newqun_bt_add:
                if (!newqun_et_xian.getText().toString().equals("")) {
                    addNewZhu();
                } else {
                    Toast.makeText(NewqunActivity.this, "请设置线数", Toast.LENGTH_SHORT).show();
                }

                break;
            default:
                break;
        }
    }

    private void saveQunId() {
        if (newqun_et_name.getText().toString().trim() != "" && !newqun_et_name.getText().equals(null)) {
            qunName = newqun_et_name.getText().toString().trim();
            if (qunName.length() > 2) {
                qunbean = new Qunbean(null, qunName);
                qunIdToZu = qunbeanDao.insert(qunbean);
                newqun_tv_id.setText("当前编号：" + qunName);
                newqun_et_name.setText("");
                Toast.makeText(NewqunActivity.this, "群添加成功", Toast.LENGTH_SHORT).show();
                zunmb = 1;
                newqun_tv_zunmb.setText("当前序列号：" + zunmb);
            } else {
                Toast.makeText(NewqunActivity.this, "群编号不合法", Toast.LENGTH_SHORT).show();
            }

        } else {
            Toast.makeText(NewqunActivity.this, "请填写群编号", Toast.LENGTH_SHORT).show();
        }

    }

    private void addNewZhu() {
        int xian = Integer.parseInt(newqun_et_xian.getText().toString().trim());
        if (qunIdToZu < 1l) {
            Toast.makeText(NewqunActivity.this, "群不存在", Toast.LENGTH_SHORT).show();
        } else if (xian % 2 == 0) {
            Toast.makeText(NewqunActivity.this, "只能单数", Toast.LENGTH_SHORT).show();
            newqun_et_xian.setText("");
        } else if (xian < 5) {
            Toast.makeText(NewqunActivity.this, "线的数量不能小于5", Toast.LENGTH_SHORT).show();
            newqun_et_xian.setText("");
        } else {
            boolean mos = newqun_rb_moss.isChecked();
            for (int i = 3; i <= xian; i++) {
                for (int j = 1; j <= xian; j++) {
                    Zubean zubean = new Zubean();
                    zubean.setMos(mos);
                    zubean.setFanganhao(fangAnHao + "-" + j);
                    zubean.setXian(i);
                    zubean.setQunId(qunIdToZu);
                    //---
                    zubeanDao.insert(zubean);
                }

            }

            Toast.makeText(NewqunActivity.this, "组添加成功", Toast.LENGTH_SHORT).show();
            newqun_tv_zunmb.setText("当前序列号：" + ++zunmb);
            newqun_et_xian.setText("");
        }


    }

    public ArrayAdapter setAdapter(List list) {
        final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
        return spinnerAdapter;
    }
}
